package com.example.lib_rongrui;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
/**
 * Created by Administrator on 2018/7/25.
 */

public class FileImport {
    private static final String XLS_FILE_PATH = Environment.getExternalStorageDirectory() + "/XLSFile/";
    private static final SimpleDateFormat DATE_FORMAT_YYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat DATE_FORMAT_DDMMYY = new SimpleDateFormat("dd/MM/yy");
    private static final SimpleDateFormat DATE_FORMAT_YYYYMMDDHHMMSS = new SimpleDateFormat("yyyy-MM-dd HHmmss");

    public static boolean daochu(String tmpname, List<HashMap<String, String>> lists2) {
        try {
            String file = tmpname.isEmpty() ? XLS_FILE_PATH + "Tag_" + getCurrentTime(DATE_FORMAT_YYYYMMDDHHMMSS) + ".xls" : XLS_FILE_PATH + tmpname;
            File path = new File(XLS_FILE_PATH);

            if (!path.exists() && !path.mkdirs()) {
                Log.i("FileImport", "Failed to create directory: " + XLS_FILE_PATH);
                return false;
            }

            List<String> headers = Collections.singletonList("EPC");
            List<Object> initialData = Collections.singletonList(headers);
            FileXls.writeXLS(file, initialData);

            List<Object> data = new ArrayList<>();
            for (HashMap<String, String> map : lists2) {
                String id = map.containsKey("tagUii") ? map.get("tagUii") : "";
                data.add(Collections.singletonList(id));
            }

            return FileXls.writeXLS(file, data);
        } catch (Exception ex) {
            Log.i("导出异常", ex.getMessage());
            return false;
        }
    }

    public static String GetTimesyyyymmdd() {
        return getCurrentTime(DATE_FORMAT_YYYYMMDD);
    }

    public static String GetTimesddMMyy() {
        return getCurrentTime(DATE_FORMAT_DDMMYY);
    }

    public static String GetTimesyyyymmddhhmmss() {
        return getCurrentTime(DATE_FORMAT_YYYYMMDDHHMMSS);
    }

    private static String getCurrentTime(SimpleDateFormat formatter) {
        return formatter.format(new Date(System.currentTimeMillis()));
    }
}
